<div class="CartWrap" id="log">

    <div class="CartNav clearfix">
     <div class="step stepbj2"><span class="step1"><{t}>购物车<{/t}></span><span class="step2"><{t}>填写订单并确认<{/t}></span><span class="step3"><{t}>成功提交订单<{/t}></span><span class="mycart"><{t}>我的购物车<{/t}></span></div>
    </div>
    <form method="post" action='<{link app=b2c ctl="site_order" act="create"}>' id="order-create">
        <{if $env.post.isfastbuy}>
        <input type='hidden' name='isfastbuy' value=1 />
        <{/if}>
        <!--<div style="display:none"><{input type="checkForm" }></div>-->
        <input type="hidden" name="purchase[member_id]" value="<{$address.member_id}>" />
        <input type="hidden" name="purchase[addr_id]" value="<{$def_addr}>" />
        <input type="hidden" name="purchase[shipping_method]" value='<{$shipping_method}>' />
        <input type="hidden" name="purchase[pay_app_id]" value="<{$payment_method}>" />
		<input type="hidden" name="purchase[def_area]" value="<{$def_area}>" />
		<input type="hidden" name="md5_cart_info" value="<{$md5_cart_info}>" />
		<input type="hidden" name="json_extend_args" value='<{$json_args}>' />

    <{include file="site/cart/checkout_base.html" app=b2c}>

	<{include file="site/cart/cart_items.html" app=b2c}>
	<{include file="site/cart/cart_solution.html" app=b2c}>
	<{include file="site/cart/cart_coupon.html" app=b2c}>


	<div class="division" >
		<h4 class="title-bg" style="height:35px; line-height:35px; overflow:hidden">
        <span style="float:right; margin-right:60px"><{t}>结算信息<{/t}></span>
        <span style="float:left; margin-top:4px">
        <button type="button" onClick="javascript:window.location='<{link app=b2c ctl="site_cart" act="index"}>'" class="btn btn-has-icon" > <span style="padding-left:10px; padding-right:0"><span style="padding-right:10px"> <i class="btn-icon"> <img src='<{$res_url}>/bundle/return_icon.gif'/></i><{t}>返回购物车<{/t}></span></span> </button></span>
  </h4>
		<div class='clearfix' id="amountInfo"><{include file="site/cart/checkout_total.html" app=b2c}></div>
		<{if $point_dis_html}><{$point_dis_html}><{/if}>
		<div align="right" style="margin:10px 50px">
         <button type="submit" class="btn order-btn"> <span><span><{t}>提交订单<{/t}></span></span> </button>
       </div>
	</div>

    <div class="CartBtn clearfix" >
        <input type="hidden" name="fromCart" value="true" />
        <!--<div class="span-auto floatRight last"><input class="actbtn btn-confirm" type="submit" value="<{t}>确认无误，下订单<{/t}>" /></div>-->
    </div>

    </form>
</div>
<script>
/*下单*/

(function(){
	window.addEvent('domready',function(){
		var f=$('order-create');
		f.removeEvents('submit');
		f.addEvent('submit', function(e){
			if (!validate(f)) {
			  e.stop();
			  return;
		  }
		});
	});

	button_valiate = {
		is_validate:function(e){
            // el element id.
			var section=e.getParent('div').getPrevious('.hid_item');
            var _validate_return = true; 
			
			return section.getElements('[vtype]').every(function(el){
                    return validate(el);
              });
        },
		fold:function(e, act){
			var section=e.getParent('.section'); 
			if (!$E('.cart-confirm-btn', section))return this;
			$E('.cart-confirm-btn', section)[act == 'fold'?'removeClass':'addClass']('unfold');			
			return this;
		},
		removeCaution:function(e){
			var section=e.getParent('.section');
			if (!$E('.cart-confirm-btn', section))return this;
			var _caution = $E('.cart-confirm-btn', section).getNext('.caution');
			if (_caution && _caution.hasClass('error')) _caution.remove();			
		},
		init:function(){		
			validatorMap.set('need_confirmed', ['<{t}>配送方式没有确认！<{/t}>', function(element, v) {
				if ($(element).hasClass('unfold')){new Fx.Scroll(window).toElement($(element.getParent('.section')));return false;}
				else return true;
			}]);
			
			validatorMap.set('shppingradiorequired', ['<{t}>配送方式不能为空！<{/t}>', function(element, v) {
				var radio = element.getParent('table').getElements('input[type=radio]');
				
				return radio.some(function(ck) {
					return ck.checked;
				});
			}]);
			
			validatorMap.set('paymentradiorequired', ['<{t}>支付方式不能为空！<{/t}>', function(element, v) {
				var radio = element.getParent('div').getElements('input[type=radio]');
				
				return radio.some(function(ck) {
					return ck.checked;
				});
			}]);
			
			validatorMap.set('mobile_or_phone', ['<{t}>手机或电话必填其一！<{/t}>', function(element, v) {
				var _mobile = $(element).getParent('tr').getPrevious('tr').getElement('input');
				var _phone = $(element).getPrevious('input');
				
				return (_mobile.value != '') || (_phone.value != '');
			}]);
		}
	}
	button_valiate.init();
	
    Order={
        init:function(){
            var _this=this;                                     
            $E('#shipping').addEvent('click',function(e){
                var target=$(e.target||e);
                switch (target.get('type')){
                    case 'radio':
					   target.checked = true;
                       _this.clearProtect(target);
                        break;
                    case 'checkbox':
                        var shipping=target.getParent('tr').getElement('input[type=radio]');
                        _this.shippingMerge(shipping,{protect:'true'},target.checked);
                        break;
                    default :break;
                }
            });
            
            $('payment').addEvent('click',function(e){
                if($(e.target).hasClass('x-payMethod'))_this.updateTotal();
            });
			
			$ES('.link_update a').addEvent('click',function(e){
				var el=e.target||e;
				_this.isEdit($(el));
			});
			
			if (!$('payment-cur'))return;	
			
			$('payment-cur').addEvent('change',function(){
				_this.setCurrency().updateTotal();
			});
		
			if (!!($E('input[name^=purchase[member_id]]').getValue()))
				$('payment-cur').fireEvent('change'); //会员还是需要调用
        },
		isEdit:function(el){
			var section=el.getParent('.section'),
				item=$ES('.hid_item',section);
			
			item.hide();
			$ES('.'+el.id, '#order-create').show();
			
			// add or remove unfold class
			if (el.get('class').indexOf('cancel_') > -1){
				button_valiate.fold(el, 'unfold');
			}
			
			if (el.get('id').indexOf('cancel_') > -1){
				button_valiate.fold(el, 'fold');
				button_valiate.removeCaution(el);
			}
		},
        setShippingFromArea:function(lastselect){		
           var _this = this;
           if(!lastselect)throw new Error('<{t}>非法的地区信息.<{/t}>');
           var _value = lastselect.value;
           var _shipping_method = '';
           if ($E('input[name^=purchase[shipping_method]]'))
            _shipping_method = $E('input[name^=purchase[shipping_method]]').getValue();
           var _isfastbuy = '<{$env.post.isfastbuy}>';
		   var _extends_args = $E('#order-create input[name^=json_extend_args]') ? $E('#order-create input[name^=json_extend_args]').value : null;
		   
			if (parseInt(_value) > 0)
            new Request.HTML({
					url:Shop.url.shipping,
					update:'shipping',
					method:'post',
					data:{area:_value, isfastbuy:_isfastbuy, shipping_method:_shipping_method, extends_args:_extends_args},
					onRequest:function(){
					  $('shipping').set('text','<{t}>正在根据地区信息获得配送方式...<{/t}>');
				   },
				   onComplete:function(){
						_this.updateTotal();					
						$('cancel_shipping_info').hide();
						if (!$('def_shipping_info').isDisplay())$('shipping_info').hide();
						
					}
				}).send();
        },
        setCurrency:function(){
            var _this = this;
            new Request.HTML({
				url:Shop.url.payment,
				update:$('payment'),
				method:'post',
				data:$H({
				   'cur':$('payment-cur').getValue(),
				   'payment':$E('#payment th input[checked]')?$E('#payment th input[checked]').value:null,
				   'd_pay':$E('#shipping th input[checked]')?$E('#shipping th input[checked]').get('has_cod'):null,
				   'def_payment':$E('input[name^=purchase[pay_app_id]]') ? $E('input[name^=purchase[pay_app_id]]').value : null,
				   'extends_args':$E('#order-create input[name^=json_extend_args]') ? $E('#order-create input[name^=json_extend_args]').value : null
				}),
				onComplete:function(){
					_this.updatePayment.call(_this);
				}
            }).send();
			return this;
        },
        updatePayment:function(){
            if(!this.synTotalHash)return;
			
			if ($E('.shipping_radio_shipping_id[checked]')){
				var pay_id=$E('input[name^=purchase[pay_app_id]]'),cod=$E('.shipping_radio_shipping_id[checked]').get('has_cod');
				
				if ((cod && pay_id.value != '-1') || (!cod && pay_id.value == '-1')){
					$('payment_info').fireEvent('click',$('payment_info'));
					$('cancel_payment_info').hide();
				}
			}
			
            if(this.synTotalHash.d_pay&&this.synTotalHash.d_pay == 'true'){
                if ($('_normal_payment'))
                    $('_normal_payment').hide();
								
                if ($('_pay_cod'))
                   $('_pay_cod').show().getElement('input[type=radio]').checked=true;
            }else{
                if ($('_normal_payment'))
                    $('_normal_payment').show();
					
                if ($('_pay_cod'))
                    $('_pay_cod').hide().getElement('input[type=radio]').checked=false;
					
				if (!$E('.x-payMethod[checked]]') && $('_normal_payment')) {$('_normal_payment').getElement('input[type=radio]').checked=true;}
            }
        },
        shippingChange:function(){			
           //this.clearProtect(target);
           this.updateTotal();
        },
        clearProtect:function(target){
           if(tmpEl=$('shipping').retrieve('tmp_protect')){
                if(tmpEl!=target){
                    tmpEl.removeProperty('protect');
                    $E('input[name^=delivery[is_protect]',tmpEl.getParent('tr')).checked=false;
                }
           }
           if(tmpEl!=target&&target.get('protect')){$('shipping').store('tmp_protect',target);}
        },
        shippingMerge:function(target,mg,checked,evt){
           if(!checked){
               $H(mg).getKeys().each(target.erase.bind(target));
           }else{
               $(target).set(mg);
               $(target).checked=true;
           }
           this.clearProtect($(target));
        },
        updateTotal:function(options){
            options = options||{};
            this.synTotalHash = (this.synTotalHash||{});

            var _shipping       = $E('#shipping tr input[type=radio]:checked'),        
				_coin           = $('payment-cur'),
				_tax            = $('is_tax'),
				_tax_company    = $E('input[name^=payment[tax_company]'),
				_dis_point		= $E('input[name^=payment[dis_point]');
				
            if(_shipping){
                $extend(this.synTotalHash,{
                    shipping_id:_shipping.value,
                    is_protect:_shipping.getParent('td').getElements('input[type=checkbox]').get('checked')?'true':'false',
                    d_pay:_shipping.get('has_cod')
                });
                this.updatePayment();
            }
            
            var _payment        = $E('#payment tr input[type=radio]:checked');
            if(_payment){
                 $extend(this.synTotalHash,{
                    payment:_payment.value
                });
            }
			
            if($E('#order-create input[name=isfastbuy]')){
                $extend(this.synTotalHash,{isfastbuy:1});
            }

            $extend(this.synTotalHash,{
                cur:_coin.getValue(),
                is_tax:(_tax&&_tax.checked)?'true':'false',
                area:$E('input[name^=delivery[ship_area]')?$E('input[name^=delivery[ship_area]').getValue():null,
                tax_company:(_tax_company != null && _tax_company != undefined) ? _tax_company.getValue() : null,
				dis_point:_dis_point ? _dis_point.value : null,
				extends_args:$E('#order-create input[name^=json_extend_args]') ? $E('#order-create input[name^=json_extend_args]').value : null				
            });
            var cart_coupon_list_modal;
			
            new Request.HTML($extend({url:Shop.url.total,update:$('amountInfo'),onRequest:function(){
					$$('#cart-coupon-list .delItem').setStyle('visibility','hidden');
			},onSuccess:function(){
				<{$point_dis_js}>
				    $$('#cart-coupon-list .delItem').fade(1);
			}},options)).post($H(this.synTotalHash));
        }
    };
    Order.init();
})();

</script>
